package com.cty._03_SolutionSeekingMindset._29_PrintMatrix;

/**
 * @Auther: cty
 * @Date: 2020/7/10 15:40
 * @Description: 面试题29：顺时针打印矩阵
 * 题目：输入一个矩阵，按照从外向里以顺时针的顺序依次打印出每一个数字。
 *
 * 行 row —— Y
 * 列 column —— X
 *
 * int[Y]
 * int[Y][X]
 * @version: 1.0
 */
public class PrintMatrix {

    public static void printMatrixClockwisely(int[][] matrix, int rows, int columns){
        if(matrix == null || rows<=0 || columns<=0)
            return;

        int start = 0;
        while(rows>start*2 && columns>start*2){
            printMatrixCircle(matrix, rows, columns, start);
            start++;
        }
        System.out.println();
    }  // end printMatrixClockwisely()

    private static void printMatrixCircle(int[][] matrix, int rows, int columns, int start){
        int endY = rows - 1 - start;
        int endX = columns - 1 - start;

        for(int i=start; i<=endX; i++)
            System.out.print(matrix[start][i] + " ");

        if(endY > start)
            for(int i=start+1; i<=endY; i++)
                System.out.print(matrix[i][endX] + " ");

        if(endY > start && endX > start)
            for(int i=endX-1; i>=start; i--)
                System.out.print(matrix[endY][i] + " ");

        if(endY > start+1 && endX > start)
            for(int i=endY-1; i>=start+1; i--)
                System.out.print(matrix[i][start] + " ");
    }  // end printMatrixCircle()

}  // end PrintMatrix()
